//
//  YBChangePassWordViewController.m
//  ZhongWangYuanApp
//
//  Created by apple on 2019/1/30.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "YBChangePassWordViewController.h"

@interface YBChangePassWordViewController ()

@property (nonatomic , strong) NSArray * titleArray;

@property (nonatomic , strong) NSArray * placeholderArray;

@property (nonatomic , strong) UITextField * ybOldPasswordTextField;

@property (nonatomic , strong) UITextField * ybNewPasswordTextField;

@property (nonatomic , strong) UITextField * ybAgainPasswordTextField;

@property (nonatomic , strong) UIButton * completeButton;

@end

@implementation YBChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titlelabel.text = @"修改密码";
    self.titleArray = @[@"旧密码" , @"新密码" , @"重复密码"];
    self.placeholderArray = @[@"旧密码" , @"请输入6位以上新密码" , @"重复输入新密码"];
    
    [self initUserInterface];
}

-(void)initUserInterface
{
    self.view.backgroundColor = [UIColor themeGrayColor];
    __block UIView * topView = nil;
    [self.titleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        contentView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:contentView];
        if (!topView) {
            contentView.sd_layout
            .leftSpaceToView(self.view, 0)
            .rightSpaceToView(self.view, 0)
            .topSpaceToView(self.navigView, ItemHeight * 20)
            .heightIs(ItemHeight * 100);
        }
        else{
            contentView.sd_layout
            .leftSpaceToView(self.view, 0)
            .rightSpaceToView(self.view, 0)
            .topSpaceToView(topView, 0)
            .heightIs(ItemHeight * 100);
        }
        topView = contentView;
        
        UILabel * itemTitleLabel = [ToolClass creatLabelWithFrame:CGRectMake(0, 0, 1, 1) font:16 textColor:[UIColor textBlackColor] text:self.titleArray[idx]];
        [contentView addSubview:itemTitleLabel];
        
        UITextField * inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        inputTextField.secureTextEntry = YES;
        inputTextField.keyboardType = UIKeyboardTypeASCIICapable;
        inputTextField.textColor = [UIColor textBlackColor];
        inputTextField.returnKeyType = UIKeyboardTypeTwitter;
//        inputTextField.delegate = self;
        inputTextField.placeholder = self.placeholderArray[idx];
        [contentView addSubview:inputTextField];
        
        UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        lineView.backgroundColor = [UIColor themeGrayColor];
        [contentView addSubview:lineView];
        
        itemTitleLabel.sd_layout
        .topSpaceToView(contentView, 0)
        .bottomSpaceToView(contentView, 0)
        .leftSpaceToView(contentView, ItemWidth * 30)
        .widthIs(ItemWidth * 150);
        
        inputTextField.sd_layout
        .topSpaceToView(contentView, 0)
        .bottomSpaceToView(contentView, 0)
        .leftSpaceToView(itemTitleLabel, 0)
        .rightSpaceToView(contentView, ItemWidth * 30);
        
        lineView.sd_layout
        .bottomSpaceToView(contentView, 0)
        .leftSpaceToView(contentView, 0)
        .rightSpaceToView(contentView, 0)
        .heightIs(normalBorderWidth);
        
        switch (idx) {
            case 0:
            {
                self.ybOldPasswordTextField = inputTextField;
                break;
            }
            case 1:
            {
                self.ybNewPasswordTextField = inputTextField;
                break;
            }
            case 2:
            {
                self.ybAgainPasswordTextField = inputTextField;
                break;
            }
            default:
                break;
        }
        
    }];
    
    [self.view addSubview:self.completeButton];
    
    self.completeButton.sd_layout
    .topSpaceToView(topView, ItemHeight * 20)
    .leftSpaceToView(self.view, ItemWidth * 30)
    .rightSpaceToView(self.view, ItemWidth * 30)
    .heightIs(ItemHeight * 88);
    
    
}

-(void)option_completeButton:(UIButton *)sender
{
    if (self.ybOldPasswordTextField.text.length < 6) {
        [YJProgressHUD showMsgWithoutView:@"密码长度必须大于6位"];
        return;
    }
    if (self.ybNewPasswordTextField.text.length < 6) {
        [YJProgressHUD showMsgWithoutView:@"密码长度必须大于6位"];
        return;
    }
    if (self.ybAgainPasswordTextField.text.length < 6) {
        [YJProgressHUD showMsgWithoutView:@"密码长度必须大于6位"];
        return;
    }
    if ([self.ybOldPasswordTextField.text isEqualToString:self.ybNewPasswordTextField.text]) {
        [YJProgressHUD showMsgWithoutView:@"新密码不能与旧密码相同"];
        return;
    }
    if (![self.ybNewPasswordTextField.text isEqualToString:self.ybAgainPasswordTextField.text]) {
        [YJProgressHUD showMsgWithoutView:@"两次密码输入不一致"];
        return;
    }
    //输入的信息都正确
    [YJProgressHUD showMessage:@"修改中..." duration:30 view:self.view];
    [YBRequestManager request_changePasswordWithOldPassword:self.ybOldPasswordTextField.text newPassword:self.ybNewPasswordTextField.text againPassword:self.ybAgainPasswordTextField.text successBlock:^(id object) {
        [YJProgressHUD hide];
        if([object[@"code"] integerValue] == 1) {
            if ([object[@"data"][@"result"] integerValue] == 1) {
                [YJProgressHUD showMessage:@"修改成功" inView:self.view compeleteBlock:^{
                    [[NSUserDefaults standardUserDefaults] setObject:self.ybNewPasswordTextField.text forKey:@"password"];
                    [self.navigationController popViewControllerAnimated:YES];
                }];
                
            }
            else{
                [YJProgressHUD showMessage:@"修改失败" inView:self.view];
            }
        }
        else{
            [YJProgressHUD showMessage:@"修改失败" inView:self.view];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMessage:@"修改失败" inView:self.view];
    }];
    
}


- (UIButton *)completeButton{
    if (_completeButton == nil){
        _completeButton = [[UIButton alloc]init];
        [_completeButton setTitle:@"完成" forState:UIControlStateNormal];
        [_completeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _completeButton.backgroundColor = [UIColor themeColor];
        _completeButton.titleLabel.font = [UIFont titleTextFont];
        _completeButton.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
        [_completeButton addTarget:self action:@selector(option_completeButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _completeButton;
}

@end
